﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Soft.Xml
{

    public class BaseConfiguration : BaseConfigFile
    {

        public BaseConfiguration(string fileName, string rootNodeName = "Configuration") : base(fileName, rootNodeName)
        {
        }


        /// <summary>
        /// 是否是Debug模式
        /// </summary>
        public bool Debug
        {
            get
            {
                return base.DocumentElement.GetAttribute("Debug").ConvertTo(false, true);
            }
            set
            {
                base.DocumentElement.SetAttribute("Debug", value.ToString());
            }
        }


        /// <summary>
        /// 实例的名称
        /// </summary>
        public string InstanceName
        {
            get
            {
                return this.GetMatchString(base.DocumentElement.GetAttribute("InstanceName"), FactoryAdvisorPlatformGlobalSet.SYSTEM_INSTANCE_NAME_MATCH_PATTERN, FactoryAdvisorPlatformGlobalSet.SYSTEM_INSTANCE_NAME_MAX_LENGTH, "SEFAJOBSERVICE").ToUpper();
            }
            set
            {
                base.DocumentElement.SetAttribute("InstanceName", this.GetMatchString(value.ToUpper(), FactoryAdvisorPlatformGlobalSet.SYSTEM_INSTANCE_NAME_MATCH_PATTERN, FactoryAdvisorPlatformGlobalSet.SYSTEM_INSTANCE_NAME_MAX_LENGTH, "SEFAJOBSERVICE"));
            }
        }


        /// <summary>
        /// 获取配置的字符串
        /// </summary>
        /// <param name="input">输入</param>
        /// <param name="pattern">匹配模式</param>
        /// <param name="maxLength">最大的长度</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns></returns>
        protected string GetMatchString(string input, string pattern, byte maxLength, string defaultValue)
        {
            Match match = Regex.Match(input, pattern);
            if (!match.Success)
            {
                return defaultValue;
            }
            if (match.Value.Length > (int)maxLength)
            {
                return match.Value.Substring(0, (int)maxLength);
            }
            return match.Value;
        }


    }
}
